﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace HooIzDat.Web.Models.ViewModels
{
    public class GamesPlayModel
    {
        public GamesPlayModel(Game game, Player currentPlayer)
        {
            Game = game;
            Player = currentPlayer;
        }

        public Game Game { get; set; }
        public Player Player { get; set; }
        public IEnumerable<SelectListItem> Questions { get; set; }

        public Character PlayerCharacter
        {
            get { return Game.Player1 == Player ? Game.Player1Character : Game.Player2Character; }
        }
        public Character OtherPlayerCharacter
        {
            get { return Game.Player1 == Player ? Game.Player2Character : Game.Player1Character; }
        }
        public IEnumerable<GameTile> PlayerTiles
        {
            get { return Game.Tiles.Where(t => t.Player == Player); }
        }
        public bool IsPlayersTurn
        {
            get { return Game.GameState != null && Game.GameState.ActivePlayer == Player; }
        }
        public bool IsWaitingForPlayer
        {
            get { return (IsPlayersTurn && (Game.IsWaitingForQuestion || Game.IsWaitingForNextTurn)) ||
                         (!IsPlayersTurn && Game.IsWaitingForAnswer); }
        }

        public string AjaxClientState
        {
            get
            {
                var state = Game.IsPending ? Game.Player1ID.ToString() : Game.GameState.ActivePlayerID.ToString();
                // Add waiting for state
                state += ",";
                state +=
                    Game.IsPending ? "IsWaitingForQuestion" :
                    !IsPlayersTurn && Game.IsWaitingForQuestion ? "IsWaitingForAnswer" :
                    !IsPlayersTurn && Game.IsWaitingForNextTurn ? "IsWaitingForQuestion" :
                    IsPlayersTurn && Game.IsWaitingForAnswer ? "IsWaitingForNextTurn" :
                    "IsWaitingForNextTurn";
                return state;
            }
        }

        public bool IsActive 
        {
            get 
            {
                return IsPlayersTurn 
                    && Game.GameState.GuessedCharacterID == null 
                    && (Game.IsWaitingForQuestion || Game.IsWaitingForNextTurn);
            }
        }
    }
}